home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Mac Game Programming Gurus / TricksOfTheMacGameProgrammingGurus.iso / CodeWarrior Lite / Metrowerks C⁄C++ Lite / Headers / STL Headers / tree.h < prev    next >
Encoding:
C/C++ Source or Header  |  1995-05-01  |  32.5 KB  |  952 lines  |  [TEXT/MMCC]

  1. /*
  2.  *
  3.  * Copyright (c) 1994
  4.  * Hewlett-Packard Company
  5.  *
  6.  * Permission to use, copy, modify, distribute and sell this software
  7.  * and its documentation for any purpose is hereby granted without fee,
  8.  * provided that the above copyright notice appear in all copies and
  9.  * that both that copyright notice and this permission notice appear
  10.  * in supporting documentation.  Hewlett-Packard Company makes no
  11.  * representations about the suitability of this software for any
  12.  * purpose.  It is provided "as is" without express or implied warranty.
  13.  *
  14.  */
  15.  
  16. #ifndef TREE_H
  17. #define TREE_H
  18.  
  19. /*
  20.  
  21. Red-black tree class, designed for use in implementing STL
  22. associative containers (set, multiset, map, and multimap). The
  23. insertion and deletion algorithms are based on those in Cormen,
  24. Leiserson, and Rivest, Introduction to Algorithms (MIT Press, 1990),
  25. except that
  26.  
  27. (1) the header cell is maintained with links not only to the root
  28. but also to the leftmost node of the tree, to enable constant time
  29. begin(), and to the rightmost node of the tree, to enable linear time
  30. performance when used with the generic set algorithms (set_union,
  31. etc.);
  32.  
  33. (2) when a node being deleted has two children its successor node is
  34. relinked into its place, rather than copied, so that the only
  35. iterators invalidated are those referring to the deleted node.
  36.  
  37. */
  38.  
  39. #include <algobase.h>
  40. #include <iterator.h>
  41. #include <function.h>
  42. #include <bool.h>
  43. #include <projectn.h>
  44.  
  45. #ifndef rb_tree 
  46. #define rb_tree rb_tree
  47. #endif
  48.  
  49. template <class Key, class Value, class KeyOfValue, class Compare>
  50. class rb_tree {
  51. protected:
  52.     enum color_type {red, black};
  53.     typedef Allocator<void>::pointer void_pointer;
  54.     struct rb_tree_node;
  55.     friend rb_tree_node;
  56.     struct rb_tree_node {
  57.         color_type color_field; 
  58.         void_pointer parent_link;
  59.         void_pointer left_link;
  60.         void_pointer right_link;
  61.         Value value_field;
  62.     };
  63.     static Allocator<rb_tree_node> rb_tree_node_allocator;
  64.     static Allocator<Value> value_allocator;
  65. public:
  66.     typedef Key key_type;
  67.     typedef Value value_type;
  68.     typedef Allocator<Value>::pointer pointer;
  69.     typedef Allocator<Value>::reference reference;
  70.     typedef Allocator<Value>::const_reference const_reference;
  71.     typedef Allocator<rb_tree_node> rb_tree_node_allocator_type;
  72.     typedef Allocator<rb_tree_node>::pointer link_type;
  73.     typedef Allocator<rb_tree_node>::size_type size_type;
  74.     typedef Allocator<rb_tree_node>::difference_type difference_type;
  75. protected:
  76.     size_type buffer_size() {
  77.         return rb_tree_node_allocator.init_page_size();
  78.     }
  79.     struct rb_tree_node_buffer;
  80.     friend rb_tree_node_buffer;
  81.     struct rb_tree_node_buffer {
  82.         void_pointer next_buffer;
  83.         link_type buffer;
  84.     };
  85. public:
  86.     typedef Allocator<rb_tree_node_buffer> buffer_allocator_type;
  87.     typedef Allocator<rb_tree_node_buffer>::pointer buffer_pointer;     
  88. protected:
  89.     static Allocator<rb_tree_node_buffer> buffer_allocator;
  90.     static buffer_pointer buffer_list;
  91.     static link_type free_list;
  92.     static link_type next_avail;
  93.     static link_type last;
  94.     void add_new_buffer() {
  95.         buffer_pointer tmp = buffer_allocator.allocate((size_type)1);
  96.         tmp->buffer = rb_tree_node_allocator.allocate(buffer_size());
  97.         tmp->next_buffer = buffer_list;
  98.         buffer_list = tmp;
  99.         next_avail = buffer_list->buffer;
  100.         last = next_avail + buffer_size();
  101.     }
  102.     static size_type number_of_trees;
  103.     void deallocate_buffers();
  104.     link_type get_node() {
  105.         link_type tmp = free_list;
  106.         return free_list ? 
  107.             (free_list = (link_type)(free_list->right_link), tmp) 
  108.                 : (next_avail == last ? (add_new_buffer(), next_avail++) 
  109.                    : next_avail++);
  110.         // ugly code for inlining - avoids multiple returns
  111.     }
  112.     void put_node(link_type p) {
  113.         p->right_link = free_list;
  114.         free_list = p;
  115.     }
  116. protected:
  117.     link_type header;  
  118.     link_type& root() { return parent(header); }
  119.     link_type& root() const { return parent(header); }
  120.     link_type& leftmost() { return left(header); }
  121.     link_type& leftmost() const { return left(header); }
  122.     link_type& rightmost() { return right(header); }
  123.     link_type& rightmost() const { return right(header); }
  124.     size_type node_count; // keeps track of size of tree
  125.     bool insert_always;  // controls whether an element already in the
  126.                          // tree is inserted again
  127. //public:
  128.     Compare key_compare;
  129.     static link_type NIL;
  130.     static link_type& left(link_type x) { 
  131.         return (link_type&)((*x).left_link);
  132.     }
  133.     static link_type& right(link_type x) {
  134.         return (link_type&)((*x).right_link); 
  135.     }
  136.     static link_type& parent(link_type x) {
  137.         return (link_type&)((*x).parent_link);
  138.     }
  139.     static reference value(link_type x) { return (*x).value_field; }
  140.     static Allocator<Key>::const_reference key(link_type x) {
  141.         return /*KeyOfValue()(value(x)*/value(x).first;
  142.     }
  143.     static color_type& color(link_type x) { 
  144.         return (color_type&)(*x).color_field; }
  145.     static link_type minimum(link_type x) {
  146.         while (left(x) != NIL)
  147.             x = left(x);
  148.         return x;
  149.     }
  150.     static link_type maximum(link_type x) {
  151.         while (right(x) != NIL)
  152.             x = right(x);
  153.         return x;
  154.     }
  155. public:
  156.     class iterator;
  157.     friend iterator;
  158.     class const_iterator;
  159.     friend const_iterator;
  160.     class iterator : public bidirectional_iterator<Value, difference_type> {
  161.     friend class rb_tree<Key, Value, KeyOfValue, Compare>;
  162.     friend class const_iterator;
  163. /*      
  164.     friend bool operator==(const iterator& x, const iterator& y) {
  165.         return x.node == y.node;
  166.     }
  167. */
  168.     protected:
  169.         link_type node;
  170.         iterator(link_type x) : node(x) {}
  171.     public:
  172.         iterator() {}
  173.         bool operator==(const iterator& y) const { return node == y.node; }
  174.         reference operator*() const { return value(node); }
  175.         iterator& operator++() {
  176.             if (right(node) != NIL) {
  177.                 node = right(node);
  178.                 while (left(node) != NIL)
  179.                     node = left(node);
  180.             } else {
  181.                 link_type y = parent(node);
  182.                 while (node == right(y)) {
  183.                     node = y;
  184.                     y = parent(y);
  185.                 }
  186.                 if (right(node) != y) // necessary because of rightmost 
  187.                     node = y;
  188.             }
  189.             return *this;
  190.         }
  191.         iterator operator++(int) {
  192.             iterator tmp = *this;
  193.             ++*this;
  194.             return tmp;
  195.         }
  196.         iterator& operator--() {
  197.             if (color(node) == red && parent(parent(node)) == node)  
  198.                 // check for header
  199.                 node = right(node);   // return rightmost
  200.             else if (left(node) != NIL) {
  201.                 link_type y = left(node);
  202.                 while (right(y) != NIL)
  203.                     y = right(y);
  204.                 node = y;
  205.             } else {
  206.                 link_type y = parent(node);
  207.                 while (node == left(y)) {
  208.                     node = y;
  209.                     y = parent(y);
  210.                 }
  211.                 node = y;
  212.             }
  213.             return *this;
  214.         }
  215.         iterator operator--(int) {
  216.             iterator tmp = *this;
  217.             --*this;
  218.             return tmp;
  219.         }
  220.     };
  221.     class const_iterator 
  222.         : public bidirectional_iterator<Value,difference_type> {
  223.     friend class rb_tree<Key, Value, KeyOfValue, Compare>;
  224.     friend class iterator;
  225. /*      
  226.     friend bool operator==(const const_iterator& x, const const_iterator& y) {
  227.         return x.node == y.node;
  228.     }
  229. */
  230.     protected:
  231.         link_type node;
  232.         const_iterator(link_type x) : node(x) {}
  233.     public:
  234.         const_iterator() {}
  235.         const_iterator(const iterator& x) : node(x.node) {}
  236.         bool operator==(const const_iterator& y) const { 
  237.             return node == y.node; 
  238.         }
  239.         bool operator!=(const const_iterator& y) const { 
  240.             return node != y.node; 
  241.         }
  242.         const_reference operator*() const { return value(node); }
  243.         const_iterator& operator++() {
  244.             if (right(node) != NIL) {
  245.                 node = right(node);
  246.                 while (left(node) != NIL)
  247.                     node = left(node);
  248.             } else {
  249.                 link_type y = parent(node);
  250.                 while (node == right(y)) {
  251.                     node = y;
  252.                     y = parent(y);
  253.                 }
  254.                 if (right(node) != y) // necessary because of rightmost 
  255.                     node = y;
  256.             }
  257.             return *this;
  258.         }
  259.         const_iterator operator++(int) {
  260.             const_iterator tmp = *this;
  261.             ++*this;
  262.             return tmp;
  263.         }
  264.         const_iterator& operator--() {
  265.             if (color(node) == red && parent(parent(node)) == node)  
  266.                 // check for header
  267.                 node = right(node);   // return rightmost
  268.             else if (left(node) != NIL) {
  269.                 link_type y = left(node);
  270.                 while (right(y) != NIL)
  271.                     y = right(y);
  272.                 node = y;
  273.             } else {
  274.                 link_type y = parent(node);
  275.                 while (node == left(y)) {
  276.                     node = y;
  277.                     y = parent(y);
  278.                 }
  279.                 node = y;
  280.             }
  281.             return *this;
  282.         }
  283.         const_iterator operator--(int) {
  284.             const_iterator tmp = *this;
  285.             --*this;
  286.             return tmp;
  287.         }
  288.     };
  289.     typedef reverse_bidirectional_iterator<iterator, value_type, reference,
  290.                                            difference_type>
  291.         reverse_iterator; 
  292.     typedef reverse_bidirectional_iterator<const_iterator, value_type,
  293.                                            const_reference, difference_type>
  294.     const_reverse_iterator;
  295. private:
  296.     iterator __insert(link_type x, link_type y, const value_type& v);
  297.     link_type __copy(link_type x, link_type p);
  298.     void __erase(link_type x);
  299.     void init() {
  300.         ++number_of_trees;
  301.         if (NIL == 0) {
  302.             NIL = get_node();
  303.             color(NIL) = black;
  304.             parent(NIL) = 0;
  305.             left(NIL) = 0;
  306.             right(NIL) = 0;
  307.         }
  308.         header = get_node();
  309.         color(header) = red;  // used to distinguish header from root,
  310.                               // in iterator.operator++
  311.         root() = NIL;
  312.         leftmost() = header;
  313.         rightmost() = header;
  314.     }
  315. public:
  316.     
  317. // allocation/deallocation
  318.     
  319.     rb_tree(const Compare& comp = Compare(), bool always = true) 
  320.            : node_count(0), key_compare(comp), insert_always(always) { 
  321.         init();
  322.     }
  323.     rb_tree(const value_type* first, const value_type* last, 
  324.             const Compare& comp = Compare(), bool always = true)
  325.           : node_count(0), key_compare(comp), insert_always(always) { 
  326.         init();
  327.         insert(first, last);
  328.     }
  329.     rb_tree(const rb_tree<Key, Value, KeyOfValue, Compare>& x, 
  330.             bool always = true) : node_count(x.node_count), 
  331.                  key_compare(x.key_compare), insert_always(always) { 
  332.         ++number_of_trees;
  333.         header = get_node();
  334.         color(header) = red;
  335.         root() = __copy(x.root(), header);
  336.         if (root() == NIL) {
  337.             leftmost() = header;
  338.             rightmost() = header;
  339.         } else {
  340.         leftmost() = minimum(root());
  341.             rightmost() = maximum(root());
  342.         }
  343.     }
  344.     ~rb_tree() {
  345.         erase(begin(), end());
  346.         put_node(header);
  347.         if (--number_of_trees == 0) {
  348.             put_node(NIL);
  349.             NIL = 0;
  350.             deallocate_buffers();
  351.             free_list = 0;    
  352.             next_avail = 0;
  353.             last = 0;
  354.         }
  355.     }
  356.     rb_tree<Key, Value, KeyOfValue, Compare>& 
  357.         operator=(const rb_tree<Key, Value, KeyOfValue, Compare>& x);
  358.     
  359. // accessors:
  360.  
  361.     Compare key_comp() const { return key_compare; }
  362.     iterator begin() { return leftmost(); }
  363.     const_iterator begin() const { return leftmost(); }
  364.     iterator end() { return header; }
  365.     const_iterator end() const { return header; }
  366.     reverse_iterator rbegin() { return reverse_iterator(end()); }
  367.     const_reverse_iterator rbegin() const { 
  368.         return const_reverse_iterator(end()); 
  369.     }
  370.     reverse_iterator rend() { return reverse_iterator(begin()); }
  371.     const_reverse_iterator rend() const { 
  372.         return const_reverse_iterator(begin());
  373.     } 
  374.     bool empty() const { return node_count == 0; }
  375.     size_type size() const { return node_count; }
  376.     size_type max_size() const { 
  377.         return rb_tree_node_allocator.max_size(); 
  378.     }
  379.     void swap(rb_tree<Key, Value, KeyOfValue, Compare>& t) {
  380.         ::swap(header, t.header);
  381.         ::swap(node_count, t.node_count);
  382.         ::swap(insert_always, t.insert_always);
  383.         ::swap(key_compare, t.key_compare);
  384.     }
  385.     
  386. // insert/erase
  387.  
  388.     typedef  pair<iterator, bool> pair_iterator_bool; 
  389.     // typedef done to get around compiler bug
  390.     pair_iterator_bool insert(const value_type& x);
  391.     iterator insert(iterator position, const value_type& x);
  392.     void insert(iterator first, iterator last);
  393.     void insert(const value_type* first, const value_type* last);
  394.     void erase(iterator position);
  395.     size_type erase(const key_type& x);
  396.     void erase(iterator first, iterator last);
  397.     void erase(const key_type* first, const key_type* last);
  398.  
  399. // set operations:
  400.  
  401.     iterator find(const key_type& x);
  402.     const_iterator find(const key_type& x) const;
  403.     size_type count(const key_type& x) const;
  404.     iterator lower_bound(const key_type& x);
  405.     const_iterator lower_bound(const key_type& x) const;
  406.     iterator upper_bound(const key_type& x);
  407.     const_iterator upper_bound(const key_type& x) const;
  408.     typedef  pair<iterator, iterator> pair_iterator_iterator; 
  409.     // typedef done to get around compiler bug
  410.     pair_iterator_iterator equal_range(const key_type& x);
  411.     typedef  pair<const_iterator, const_iterator> pair_citerator_citerator; 
  412.     // typedef done to get around compiler bug
  413.     pair_citerator_citerator equal_range(const key_type& x) const;
  414.     inline void rotate_left(link_type x);
  415.     inline void rotate_right(link_type x);
  416. };
  417.  
  418. template <class Key, class Value, class KeyOfValue, class Compare>
  419. rb_tree<Key, Value, KeyOfValue, Compare>::buffer_pointer 
  420.         rb_tree<Key, Value, KeyOfValue, Compare>::buffer_list = 0;
  421.  
  422. template <class Key, class Value, class KeyOfValue, class Compare>
  423. rb_tree<Key, Value, KeyOfValue, Compare>::link_type 
  424.         rb_tree<Key, Value, KeyOfValue, Compare>::free_list = 0;
  425.  
  426. template <class Key, class Value, class KeyOfValue, class Compare>
  427. rb_tree<Key, Value, KeyOfValue, Compare>::link_type 
  428.         rb_tree<Key, Value, KeyOfValue, Compare>::next_avail = 0;
  429.  
  430. template <class Key, class Value, class KeyOfValue, class Compare>
  431. rb_tree<Key, Value, KeyOfValue, Compare>::link_type 
  432.         rb_tree<Key, Value, KeyOfValue, Compare>::last = 0;
  433.  
  434. template <class Key, class Value, class KeyOfValue, class Compare>
  435. rb_tree<Key, Value, KeyOfValue, Compare>::size_type 
  436.         rb_tree<Key, Value, KeyOfValue, Compare>::number_of_trees = 0;
  437.  
  438. template <class Key, class Value, class KeyOfValue, class Compare>
  439. rb_tree<Key, Value, KeyOfValue, Compare>::rb_tree_node_allocator_type 
  440.         rb_tree<Key, Value, KeyOfValue, Compare>::rb_tree_node_allocator;
  441.  
  442. template <class Key, class Value, class KeyOfValue, class Compare>
  443. Allocator<Value> rb_tree<Key, Value, KeyOfValue, Compare>::value_allocator;
  444.  
  445. template <class Key, class Value, class KeyOfValue, class Compare>
  446. rb_tree<Key, Value, KeyOfValue, Compare>::buffer_allocator_type 
  447.         rb_tree<Key, Value, KeyOfValue, Compare>::buffer_allocator;
  448.  
  449. template <class Key, class Value, class KeyOfValue, class Compare>
  450. rb_tree<Key, Value, KeyOfValue, Compare>::link_type 
  451.         rb_tree<Key, Value, KeyOfValue, Compare>::NIL = 0;
  452.  
  453. template <class Key, class Value, class KeyOfValue, class Compare>
  454. void rb_tree<Key, Value, KeyOfValue, Compare>::deallocate_buffers() {
  455.     while (buffer_list) {
  456.         buffer_pointer tmp = buffer_list;
  457.         buffer_list = (buffer_pointer)(buffer_list->next_buffer);
  458.         rb_tree_node_allocator.deallocate(tmp->buffer);
  459.         buffer_allocator.deallocate(tmp);
  460.     }
  461. }
  462.  
  463. template <class Key, class Value, class KeyOfValue, class Compare>
  464. inline bool operator==(const rb_tree<Key, Value, KeyOfValue, Compare>& x, 
  465.                        const rb_tree<Key, Value, KeyOfValue, Compare>& y) {
  466.     return x.size() == y.size() && equal(x.begin(), x.end(), y.begin());
  467. }
  468.  
  469. template <class Key, class Value, class KeyOfValue, class Compare>
  470. inline bool operator<(const rb_tree<Key, Value, KeyOfValue, Compare>& x, 
  471.                       const rb_tree<Key, Value, KeyOfValue, Compare>& y) {
  472.     return lexicographical_compare(x.begin(), x.end(), y.begin(), y.end());
  473. }
  474.  
  475. template <class Key, class Value, class KeyOfValue, class Compare>
  476. rb_tree<Key, Value, KeyOfValue, Compare>& 
  477. rb_tree<Key, Value, KeyOfValue, Compare>::
  478. operator=(const rb_tree<Key, Value, KeyOfValue, Compare>& x) {
  479.     if (this != &x) {
  480.         // can't be done as in list because Key may be a constant type
  481.         erase(begin(), end());
  482.         root() = __copy(x.root(), header);
  483.         if (root() == NIL) {
  484.             leftmost() = header;
  485.             rightmost() = header;
  486.         } else {
  487.         leftmost() = minimum(root());
  488.             rightmost() = maximum(root());
  489.         }
  490.         node_count = x.node_count;
  491.     }
  492.     return *this;
  493. }
  494.  
  495. template <class Key, class Value, class KeyOfValue, class Compare>
  496. rb_tree<Key, Value, KeyOfValue, Compare>::iterator
  497. rb_tree<Key, Value, KeyOfValue, Compare>::
  498. __insert(link_type x, link_type y, const Value& v) {
  499.     ++node_count;
  500.     link_type z = get_node();
  501.     construct(value_allocator.address(value(z)), v);
  502.     if (y == header || x != NIL || key_compare(/*KeyOfValue()(v)*/v.first, key(y))) {
  503.         left(y) = z;  // also makes leftmost() = z when y == header
  504.         if (y == header) {
  505.             root() = z;
  506.             rightmost() = z;
  507.         } else if (y == leftmost())
  508.             leftmost() = z;   // maintain leftmost() pointing to minimum node
  509.     } else {
  510.         right(y) = z;
  511.         if (y == rightmost())
  512.             rightmost() = z;   // maintain rightmost() pointing to maximum node
  513.     }
  514.     parent(z) = y;
  515.     left(z) = NIL;
  516.     right(z) = NIL;
  517.     x = z;  // recolor and rebalance the tree
  518.     color(x) = red;
  519.     while (x != root() && color(parent(x)) == red) 
  520.         if (parent(x) == left(parent(parent(x)))) {
  521.             y = right(parent(parent(x)));
  522.             if (color(y) == red) {
  523.                 color(parent(x)) = black;
  524.                 color(y) = black;
  525.                 color(parent(parent(x))) = red;
  526.                 x = parent(parent(x));
  527.             } else {
  528.                 if (x == right(parent(x))) {
  529.                     x = parent(x);
  530.                     rotate_left(x);
  531.                 }
  532.                 color(parent(x)) = black;
  533.                 color(parent(parent(x))) = red;
  534.                 rotate_right(parent(parent(x)));
  535.             }
  536.         } else {
  537.             y = left(parent(parent(x)));
  538.             if (color(y) == red) {
  539.                 color(parent(x)) = black;
  540.                 color(y) = black;
  541.                 color(parent(parent(x))) = red;
  542.                 x = parent(parent(x));
  543.             } else {
  544.                 if (x == left(parent(x))) {
  545.                     x = parent(x);
  546.                     rotate_right(x);
  547.                 }
  548.                 color(parent(x)) = black;
  549.                 color(parent(parent(x))) = red;
  550.                 rotate_left(parent(parent(x)));
  551.             }
  552.         }
  553.     color(root()) = black;
  554.     return iterator(z);
  555. }
  556.  
  557. template <class Key, class Value, class KeyOfValue, class Compare>
  558. rb_tree<Key, Value, KeyOfValue, Compare>::pair_iterator_bool
  559. rb_tree<Key, Value, KeyOfValue, Compare>::insert(const Value& v) {
  560.     link_type y = header;
  561.     link_type x = root();
  562.     bool comp = true;
  563.     while (x != NIL) {
  564.         y = x;
  565.         comp = key_compare(/*KeyOfValue()(v)*/v.first, key(x));
  566.         x = comp ? left(x) : right(x);
  567.     }
  568.     if (insert_always)
  569.         return pair_iterator_bool(__insert(x, y, v), true);
  570.     iterator j = iterator(y);   
  571.     if (comp)
  572.         if (j == begin())     
  573.             return pair_iterator_bool(__insert(x, y, v), true);
  574.         else
  575.             --j;
  576.     if (key_compare(key(j.node), /*KeyOfValue()(v)*/v.first))
  577.         return pair_iterator_bool(__insert(x, y, v), true);
  578.     return pair_iterator_bool(j, false);
  579. }
  580.  
  581. template <class Key, class Value, class KeyOfValue, class Compare>
  582. rb_tree<Key, Value, KeyOfValue, Compare>::iterator 
  583. rb_tree<Key, Value, KeyOfValue, Compare>::insert(iterator position,
  584.                                                  const Value& v) {
  585.     if (position == iterator(begin()))
  586.         if (size() > 0 && key_compare(KeyOfValue()(v), key(position.node)))
  587.             return __insert(position.node, position.node, v);
  588.             // first argument just needs to be non-NIL 
  589.         else
  590.             return insert(v).first;
  591.     else if (position == iterator(end()))
  592.         if (key_compare(key(rightmost()), KeyOfValue()(v)))
  593.             return __insert(NIL, rightmost(), v);
  594.         else
  595.             return insert(v).first;
  596.     else {
  597.         iterator before = --position;
  598.         if (key_compare(key(before.node), KeyOfValue()(v))
  599.             && key_compare(KeyOfValue()(v), key(position.node)))
  600.             if (right(before.node) == NIL)
  601.                 return __insert(NIL, before.node, v); 
  602.             else
  603.                 return __insert(position.node, position.node, v);
  604.                 // first argument just needs to be non-NIL 
  605.         else
  606.             return insert(v).first;
  607.     }
  608. }
  609.  
  610. template <class Key, class Value, class KeyOfValue, class Compare>
  611. void rb_tree<Key, Value, KeyOfValue, Compare>::insert(iterator first, 
  612.                                                       iterator last) {
  613.     while (first != last) insert(*first++);
  614. }
  615.  
  616. template <class Key, class Value, class KeyOfValue, class Compare>
  617. void rb_tree<Key, Value, KeyOfValue, Compare>::insert(const Value* first, 
  618.                                                       const Value* last) {
  619.     while (first != last) insert(*first++);
  620. }
  621.          
  622. template <class Key, class Value, class KeyOfValue, class Compare>
  623. void rb_tree<Key, Value, KeyOfValue, Compare>::erase(iterator position) {
  624.     link_type z = position.node;
  625.     link_type y = z;
  626.     link_type x;
  627.     if (left(y) == NIL)
  628.         x = right(y);
  629.     else
  630.         if (right(y) == NIL) 
  631.             x = left(y);
  632.         else {
  633.             y = right(y);
  634.             while (left(y) != NIL)
  635.                 y = left(y);
  636.             x = right(y);
  637.         }
  638.     if (y != z) { // relink y in place of z
  639.         parent(left(z)) = y; 
  640.         left(y) = left(z);
  641.         if (y != right(z)) {
  642.             parent(x) = parent(y); // possibly x == NIL
  643.             left(parent(y)) = x;   // y must be a left child
  644.             right(y) = right(z);
  645.             parent(right(z)) = y;
  646.         } else
  647.             parent(x) = y;  // needed in case x == NIL
  648.         if (root() == z)
  649.             root() = y;
  650.         else if (left(parent(z)) == z)
  651.             left(parent(z)) = y;
  652.         else 
  653.             right(parent(z)) = y;
  654.         parent(y) = parent(z);
  655.         ::swap(color(y), color(z));
  656.         ::swap(y, z);  
  657.                        // y points to node to be actually deleted,
  658.                        // z points to old z's former successor
  659.     } else {  // y == z
  660.         parent(x) = parent(y);   // possibly x == NIL
  661.         if (root() == z)
  662.             root() = x;
  663.         else 
  664.             if (left(parent(z)) == z)
  665.                 left(parent(z)) = x;
  666.             else
  667.                 right(parent(z)) = x;
  668.         if (leftmost() == z) 
  669.             if (right(z) == NIL)  // left(z) must be NIL also
  670.                 leftmost() = parent(z);
  671.                 // makes leftmost() == header if z == root()
  672.         else
  673.             leftmost() = minimum(x);
  674.         if (rightmost() == z)  
  675.             if (left(z) == NIL) // right(z) must be NIL also
  676.                 rightmost() = parent(z);  
  677.                 // makes rightmost() == header if z == root()
  678.         else  // x == left(z)
  679.             rightmost() = maximum(x);
  680.     }
  681.     if (color(y) != red) { 
  682.         while (x != root() && color(x) == black)
  683.             if (x == left(parent(x))) {
  684.                 link_type w = right(parent(x));
  685.                 if (color(w) == red) {
  686.                     color(w) = black;
  687.                     color(parent(x)) = red;
  688.                     rotate_left(parent(x));
  689.                     w = right(parent(x));
  690.                 }
  691.                 if (color(left(w)) == black && color(right(w)) == black) {
  692.                     color(w) = red;
  693.                     x = parent(x);
  694.                 } else {
  695.                     if (color(right(w)) == black) {
  696.                         color(left(w)) = black;
  697.                         color(w) = red;
  698.                         rotate_right(w);
  699.                         w = right(parent(x));
  700.                     }
  701.                     color(w) = color(parent(x));
  702.                     color(parent(x)) = black;
  703.                     color(right(w)) = black;
  704.                     rotate_left(parent(x));
  705.                     break;
  706.                 }
  707.             } else {  // same as then clause with "right" and "left" exchanged
  708.                 link_type w = left(parent(x));
  709.                 if (color(w) == red) {
  710.                     color(w) = black;
  711.                     color(parent(x)) = red;
  712.                     rotate_right(parent(x));
  713.                     w = left(parent(x));
  714.                 }
  715.                 if (color(right(w)) == black && color(left(w)) == black) {
  716.                     color(w) = red;
  717.                     x = parent(x);
  718.                 } else {
  719.                     if (color(left(w)) == black) {
  720.                         color(right(w)) = black;
  721.                         color(w) = red;
  722.                         rotate_left(w);
  723.                         w = left(parent(x));
  724.                     }
  725.                     color(w) = color(parent(x));
  726.                     color(parent(x)) = black;
  727.                     color(left(w)) = black;
  728.                     rotate_right(parent(x));
  729.                     break;
  730.                 }
  731.             }
  732.         color(x) = black;
  733.     }
  734.     destroy(value_allocator.address(value(y)));
  735.     put_node(y);
  736.     --node_count;
  737. }
  738.  
  739. template <class Key, class Value, class KeyOfValue, class Compare>
  740. rb_tree<Key, Value, KeyOfValue, Compare>::size_type 
  741. rb_tree<Key, Value, KeyOfValue, Compare>::erase(const Key& x) {
  742.     pair_iterator_iterator p = equal_range(x);
  743.     size_type n = 0;
  744.     distance(p.first, p.second, n);
  745.     erase(p.first, p.second);
  746.     return n;
  747. }
  748.  
  749. template <class Key, class Value, class KeyOfValue, class Compare>
  750. rb_tree<Key, Value, KeyOfValue, Compare>::link_type 
  751. rb_tree<Key, Value, KeyOfValue, Compare>::__copy(link_type x, link_type p) {
  752.    // structural copy
  753.    link_type r = x;
  754.    while (x != NIL) {
  755.       link_type y = get_node();
  756.       if (r == x) r = y;  // save for return value
  757.       construct(value_allocator.address(value(y)), value(x));
  758.       left(p) = y;
  759.       parent(y) = p;
  760.       color(y) = color(x);
  761.       right(y) = __copy(right(x), y);
  762.       p = y;
  763.       x = left(x);
  764.    }
  765.    left(p) = NIL;
  766.    return r;
  767. }
  768.  
  769. template <class Key, class Value, class KeyOfValue, class Compare>
  770. void rb_tree<Key, Value, KeyOfValue, Compare>::__erase(link_type x) {
  771.     // erase without rebalancing
  772.     while (x != NIL) {
  773.        __erase(right(x));
  774.        link_type y = left(x);
  775.        destroy(value_allocator.address(value(x)));
  776.        put_node(x);
  777.        x = y;
  778.     }
  779. }
  780.  
  781. template <class Key, class Value, class KeyOfValue, class Compare>
  782. void rb_tree<Key, Value, KeyOfValue, Compare>::erase(iterator first, 
  783.                                                      iterator last) {
  784.     if (first == begin() && last == end() && node_count != 0) {
  785.         __erase(root());
  786.         leftmost() = header;
  787.         root() = NIL;
  788.         rightmost() = header;
  789.         node_count = 0;
  790.     } else
  791.         while (first != last) erase(first++);
  792. }
  793.  
  794. template <class Key, class Value, class KeyOfValue, class Compare>
  795. void rb_tree<Key, Value, KeyOfValue, Compare>::erase(const Key* first, 
  796.                                                      const Key* last) {
  797.     while (first != last) erase(*first++);
  798. }
  799.  
  800. template <class Key, class Value, class KeyOfValue, class Compare>
  801. rb_tree<Key, Value, KeyOfValue, Compare>::iterator 
  802. rb_tree<Key, Value, KeyOfValue, Compare>::find(const Key& k) {
  803.     link_type y = header;
  804.     link_type x = root();
  805.     bool comp = false;
  806.     while (x != NIL) {
  807.         y = x;
  808.         comp = key_compare(key(x), k);
  809.         x = comp ? right(x) : left(x);
  810.     }
  811.     iterator j = iterator(y);   
  812.     if (comp) ++j;
  813.     return (j == end() || key_compare(k, key(j.node))) ? end() : j;
  814. }
  815.  
  816. template <class Key, class Value, class KeyOfValue, class Compare>
  817. rb_tree<Key, Value, KeyOfValue, Compare>::const_iterator 
  818. rb_tree<Key, Value, KeyOfValue, Compare>::find(const Key& k) const {
  819.     link_type y = header;
  820.     link_type x = root();
  821.     bool comp = false;
  822.     while (x != NIL) {
  823.         y = x;
  824.         comp = key_compare(key(x), k);
  825.         x = comp ? right(x) : left(x);
  826.     }
  827.     const_iterator j = const_iterator(y);   
  828.     if (comp) ++j;
  829.     return (j == end() || key_compare(k, key(j.node))) ? end() : j;
  830. }
  831.  
  832. template <class Key, class Value, class KeyOfValue, class Compare>
  833. rb_tree<Key, Value, KeyOfValue, Compare>::size_type 
  834. rb_tree<Key, Value, KeyOfValue, Compare>::count(const Key& k) const {
  835.     pair<const_iterator, const_iterator> p = equal_range(k);
  836.     size_type n = 0;
  837.     distance(p.first, p.second, n);
  838.     return n;
  839. }
  840.  
  841. template <class Key, class Value, class KeyOfValue, class Compare>
  842. rb_tree<Key, Value, KeyOfValue, Compare>::iterator 
  843. rb_tree<Key, Value, KeyOfValue, Compare>::lower_bound(const Key& k) {
  844.     link_type y = header;
  845.     link_type x = root();
  846.     bool comp = false;
  847.     while (x != NIL) {
  848.         y = x;
  849.         comp = key_compare(key(x), k);
  850.         x = comp ? right(x) : left(x);
  851.     }
  852.     iterator j = iterator(y);   
  853.     return comp ? ++j : j;
  854. }
  855.  
  856. template <class Key, class Value, class KeyOfValue, class Compare>
  857. rb_tree<Key, Value, KeyOfValue, Compare>::const_iterator 
  858. rb_tree<Key, Value, KeyOfValue, Compare>::lower_bound(const Key& k) const {
  859.     link_type y = header;
  860.     link_type x = root();
  861.     bool comp = false;
  862.     while (x != NIL) {
  863.         y = x;
  864.         comp = key_compare(key(x), k);
  865.         x = comp ? right(x) : left(x);
  866.     }
  867.     const_iterator j = const_iterator(y);   
  868.     return comp ? ++j : j;
  869. }
  870.  
  871. template <class Key, class Value, class KeyOfValue, class Compare>
  872. rb_tree<Key, Value, KeyOfValue, Compare>::iterator 
  873. rb_tree<Key, Value, KeyOfValue, Compare>::upper_bound(const Key& k) {
  874.     link_type y = header;
  875.     link_type x = root();
  876.     bool comp = true;
  877.     while (x != NIL) {
  878.         y = x;
  879.         comp = key_compare(k, key(x));
  880.         x = comp ? left(x) : right(x);
  881.     }
  882.     iterator j = iterator(y);   
  883.     return comp ? j : ++j;
  884. }
  885.  
  886. template <class Key, class Value, class KeyOfValue, class Compare>
  887. rb_tree<Key, Value, KeyOfValue, Compare>::const_iterator 
  888. rb_tree<Key, Value, KeyOfValue, Compare>::upper_bound(const Key& k) const {
  889.     link_type y = header;
  890.     link_type x = root();
  891.     bool comp = true;
  892.     while (x != NIL) {
  893.         y = x;
  894.         comp = key_compare(k, key(x));
  895.         x = comp ? left(x) : right(x);
  896.     }
  897.     const_iterator j = const_iterator(y);   
  898.     return comp ? j : ++j;
  899. }
  900.  
  901.  
  902. template <class Key, class Value, class KeyOfValue, class Compare>
  903. rb_tree<Key, Value, KeyOfValue, Compare>::pair_iterator_iterator 
  904. rb_tree<Key, Value, KeyOfValue, Compare>::equal_range(const Key& k) {
  905.     return pair_iterator_iterator(lower_bound(k), upper_bound(k));
  906. }
  907.  
  908. template <class Key, class Value, class KeyOfValue, class Compare>
  909. rb_tree<Key, Value, KeyOfValue, Compare>::pair_citerator_citerator 
  910. rb_tree<Key, Value, KeyOfValue, Compare>::equal_range(const Key& k) const {
  911.     return pair_citerator_citerator(lower_bound(k), upper_bound(k));
  912. }
  913.  
  914. template <class Key, class Value, class KeyOfValue, class Compare>
  915. inline void 
  916. rb_tree<Key, Value, KeyOfValue, Compare>::rotate_left(link_type x) {
  917.     link_type y = right(x);
  918.     right(x) = left(y);
  919.     if (left(y) != NIL)
  920.         parent(left(y)) = x;
  921.     parent(y) = parent(x);
  922.     if (x == root())
  923.         root() = y;
  924.     else if (x == left(parent(x)))
  925.         left(parent(x)) = y;
  926.     else
  927.         right(parent(x)) = y;
  928.     left(y) = x;
  929.     parent(x) = y;
  930. }
  931.  
  932. template <class Key, class Value, class KeyOfValue, class Compare>
  933. inline void 
  934. rb_tree<Key, Value, KeyOfValue, Compare>::rotate_right(link_type x) {
  935.     link_type y = left(x);
  936.     left(x) = right(y);
  937.     if (right(y) != NIL)
  938.         parent(right(y)) = x;
  939.     parent(y) = parent(x);
  940.     if (x == root())
  941.         root() = y;
  942.     else if (x == right(parent(x)))
  943.         right(parent(x)) = y;
  944.     else
  945.         left(parent(x)) = y;
  946.     right(y) = x;
  947.     parent(x) = y;
  948. }
  949.  
  950. #endif
  951.  
  952.